package mi.payment.ui.print;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Locale;

import mi.payment.R;
import mi.payment.db.dao.PayDetail;
import mi.payment.transaction.AutoFinishActivity;
import sunmi.sunmiui.dialog.DialogCreater;
import sunmi.sunmiui.dialog.EditTextDialog;
import sunmi.sunmiui.dialog.LoadingDialog;
import sunmi.sunmiui.title.Title;

/**
 * 按凭证号打印
 *
 * @author Xiho on 2017/1/10.
 */

public class PrintByDocumentNumberActivity extends AutoFinishActivity {

    // 凭证号打印
    private TextView mTextPrintByDocument;
    private RelativeLayout mRelOk;
    // 流水号
    private String orderNumber;

    private EditTextDialog mEditTextDialog;

    private LoadingDialog loadingDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_print_by_document);
        initView();
    }

    /**
     * 初始化View
     */
    private void initView() {
        mRelOk = (RelativeLayout) findViewById(R.id.rel_ok);
        View printByDocumentNumView = findViewById(R.id.print_by_document_num);
        TextView tvLeft = (TextView) printByDocumentNumView.findViewById(R.id.item_left_text);
        tvLeft.setText(getString(R.string.by_document_number));
        mTextPrintByDocument = (TextView) printByDocumentNumView.findViewById(R.id.item_right_text);
        mTextPrintByDocument.setText(getString(R.string.please_enter_the_order_number));

        printByDocumentNumView.setOnClickListener(this);

        Title title = (Title) findViewById(R.id.print_by_document_num_title);
        title.setButtonVisibility(false, true, true, true);
        title.setHeadBgColor(getResources().getColor(R.color.colorBar));
        title.setTitleText(getResources().getString(R.string.tran_print_by_document_number));
        title.setMenuClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }

        }, null, null, null);

        checkParamsLength();
    }


    private void checkParamsLength() {
        if (orderNumber != null && orderNumber.length() > 0) {
            mRelOk.setBackgroundResource(R.drawable.bg_corner_primary);
            mRelOk.setOnClickListener(this);
        } else {
            mRelOk.setBackgroundResource(R.drawable.bg_gray_corner_login_btn);
            mRelOk.setOnClickListener(null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.print_by_document_num:
                showEditOrderNumberDialog();
                break;
            case R.id.rel_ok:
                if (TextUtils.isEmpty(orderNumber)) {
                    showToast(getString(R.string.please_enter_the_order_number));
                } else {
                    loadingDialog = DialogCreater.createLoadingDialog(this, getString(R.string.search));
                    loadingDialog.show();
                    executeTask(new Runnable() {

                        @Override
                        public void run() {
                            final PayDetail pay = mPayDetailModel.getSuccessPayDetailByVoucherNo(orderNumber);
                            runOnUI(new Runnable() {

                                @Override
                                public void run() {
                                    loadingDialog.dismiss();
                                    if (pay != null) {
                                        FillInTheCertificateActivity.startAction(PrintByDocumentNumberActivity.this, orderNumber);
                                        finish();
                                    } else {
                                        showToast(getResources().getString(R.string.no_this_transaction));
                                    }
                                }

                            });

                        }

                    });
                }
                break;
        }
    }

    /**
     * 输入订单的编号的Dialog
     */
    private void showEditOrderNumberDialog() {
        mEditTextDialog = DialogCreater.createEditTextDialog(this, getString(R.string.cancel),
                getString(R.string.save), getString(R.string.by_document_number),
                new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        mEditTextDialog.dismiss();
                    }

                }, new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        orderNumber = mEditTextDialog.getText();
                        String format = "%06d";
                        if (!TextUtils.isEmpty(orderNumber)) {
                            orderNumber=String.format(Locale.getDefault(), format, Long.parseLong(orderNumber));
                            mTextPrintByDocument.setText(orderNumber);
                        }
                        checkParamsLength();
                        mEditTextDialog.dismiss();
                    }

                }, new DialogInterface.OnShowListener() {

                    @Override
                    public void onShow(DialogInterface dialog) {
                        mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                        mEditTextDialog.showSoftInput();
                    }

                });
        if (!mTextPrintByDocument.getText().toString().equals(getResources().getString(R.string.please_enter_the_order_number))) {
            mEditTextDialog.setText(mTextPrintByDocument.getText().toString());
        }
        mEditTextDialog.setMaxLength(6);
        mEditTextDialog.show();
    }


    @Override
    protected void onDestroy() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
        if (mEditTextDialog != null) {
            mEditTextDialog.dismiss();
            mEditTextDialog = null;
        }
        super.onDestroy();
    }


}
